﻿using System;
using System.Runtime.InteropServices;

namespace EarthWorm
{
    /// <summary>
    /// SDK 的 Local 数据信息，使用 ini 本地存储
    /// </summary>
    class DataManager
    {
        public const string INI_ROOT_KEY = "BHUpdater";
        public const string INI_KEY_EXE_NAME = "INI_KEY_EXE_NAME";
        public const string INI_KEY_EXE_TARGET = "INI_KEY_EXE_TARGET";
        public const string INI_KEY_INI_SERVER_URL = "INI_KEY_INI_SERVER_URL";
        public const string INI_KEY_REMOTE_VER_FILE = "INI_KEY_REMOTE_VER_FILE";
        public const string INI_KEY_REMOTE_DOWNLOAD_URL_FILE = "INI_KEY_REMOTE_DOWNLOAD_URL_FILE";
        public const string INI_KEY_LOCAL_DIR = "INI_KEY_LOCAL_DIR";
        public const string INI_KEY_LOCAL_VER_FILE = "INI_KEY_LOCAL_VER_FILE";

        private IniStorage storage;

        private DataManager()
        {
            storage = new IniStorage(@".\BHUpdater.ini");
        }

        public void InitConfig()
        {
            string param;
            param = storage.GetValue(INI_ROOT_KEY, INI_KEY_EXE_NAME);
            if (string.IsNullOrEmpty(param))
                this.mExeName = @"计算器";
            else
                this.mExeName = param;

            param = storage.GetValue(INI_ROOT_KEY, INI_KEY_EXE_TARGET);
            if (string.IsNullOrEmpty(param))
                this.mExeTarget = @"calc.exe";
            else
                this.mExeTarget = param;

            param = storage.GetValue(INI_ROOT_KEY, INI_KEY_INI_SERVER_URL);
            if (string.IsNullOrEmpty(param))
                this.mIniServerURL = @"https://servereaddress/计算器/";
            else
                this.mIniServerURL = param;

            param = storage.GetValue(INI_ROOT_KEY, INI_KEY_REMOTE_DOWNLOAD_URL_FILE);
            if (string.IsNullOrEmpty(param))
                this.mRemoteDownloadUrlFile = @"downloadUrl.txt";
            else
                this.mRemoteDownloadUrlFile = param;

            param = storage.GetValue(INI_ROOT_KEY, INI_KEY_REMOTE_VER_FILE);
            if (string.IsNullOrEmpty(param))
                this.mRemoteVerFile = @"updateVersion.txt";
            else
                this.mRemoteVerFile = param;

            param = storage.GetValue(INI_ROOT_KEY, INI_KEY_LOCAL_DIR);
            if (string.IsNullOrEmpty(param))
                this.mLocalDir = @"Downloads";
            else
                this.mLocalDir = param;

            param = storage.GetValue(INI_ROOT_KEY, INI_KEY_LOCAL_VER_FILE);
            if (string.IsNullOrEmpty(param))
                this.mLocalVerFile = @"version.txt";
            else
                this.mLocalVerFile = param;
        }

        public void Uninit()
        {
            WriteConfig();
        }

        private void WriteConfig()
        {
            storage.SetValue(INI_ROOT_KEY, INI_KEY_EXE_NAME, this.mExeName);
            storage.SetValue(INI_ROOT_KEY, INI_KEY_EXE_TARGET, this.mExeTarget);
            storage.SetValue(INI_ROOT_KEY, INI_KEY_INI_SERVER_URL, this.mIniServerURL);
            storage.SetValue(INI_ROOT_KEY, INI_KEY_REMOTE_VER_FILE, this.mRemoteVerFile);
            storage.SetValue(INI_ROOT_KEY, INI_KEY_LOCAL_DIR, this.mLocalDir);
            storage.SetValue(INI_ROOT_KEY, INI_KEY_LOCAL_VER_FILE, this.mLocalVerFile);
            storage.SetValue(INI_ROOT_KEY, INI_KEY_REMOTE_DOWNLOAD_URL_FILE, this.mRemoteDownloadUrlFile);
        }

        private static DataManager mInstance;
        private static readonly object padlock = new object();
        public static DataManager GetInstance()
        {
            if (mInstance == null)
            {
                lock (padlock)
                {
                    if (mInstance == null)
                    {
                        mInstance = new DataManager();
                    }
                }
            }
            return mInstance;
        }

        #region 设置参数
        public string mExeName { get; set; }
        public string mExeTarget { get; set; }
        public string mIniServerURL { get; set; }
        public string mRemoteVerFile { get; set; }
        public string mRemoteDownloadUrlFile { get; set; }
        public string mLocalDir { get; set; }
        public string mLocalVerFile { get; set; }
        #endregion
    }


    class IniStorage
    {
        // 声明INI文件的写操作函数 
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        // 声明INI文件的读操作函数 
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        private string sPath = null;

        public IniStorage(string path)
        {
            this.sPath = path;
        }

        public void SetValue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }

        public string GetValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
    }
}
